﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common.EventHandler;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Common
{
   public class UserControlledSprite:Sprite
   {
       /// <summary>
       /// 鼠标左键单击事件
       /// </summary>
       public event ClickLeftEventHandler ClickLeftEvent;
       /// <summary>
       /// 鼠标右键单击事件
       /// </summary>
       public event ClickRightEventHandler ClickRightEvent;
       /// <summary>
       /// 精灵鼠标移入事件
       /// </summary>
       public event MoveOnEventHandler MoveOnEvent;
       /// <summary>
       /// 精灵鼠标移出事件
       /// </summary>
       public event MoveOutEventHandler MoveOutEvent;
       /// <summary>
       /// 是否显示
       /// </summary>
       protected bool Showing;
      /// <summary>
      /// 当前鼠标状态
      /// </summary>
       public static MouseState MouseState;
       /// <summary>
       /// 鼠标左键前一个按键状态Pressed Or Released
       /// </summary>
       public static ButtonState PreMouseStateLeftButton;
       /// <summary>
       /// 鼠标左键是否已经按下,方便处理单击事件
       /// </summary>
       public bool IsLeftButtonPressed;
       /// <summary>
       /// 鼠标右键是否已经按下,方便处理单击事件
       /// </summary>
       public bool IsRightButtonPressed;
       /// <summary>
       /// 鼠标是否已经移动到目标区域
       /// </summary>
       public bool IsMoveOn;
       /// <summary>
       /// 鼠标是否已经移出目标区域
       /// </summary>
       public bool IsMoveOut;
       /// <summary>
       /// 自动归位用户控制精灵鼠标按键释放状态
       /// </summary>
       public static void AutoReleasedMouseStateForButton()
       {
           MouseState mouseState = Mouse.GetState();
           if (mouseState.LeftButton == ButtonState.Released)
               UserControlledSprite.PreMouseStateLeftButton = ButtonState.Released;
       }
       /// <summary>
       /// 控制精灵是否显示
       /// </summary>
       public void ChangeShowing(object args)
       {
           this.Showing = !this.Showing;
       }
       /// <summary>
       /// 控制精灵是否显示
       /// </summary>
       public void SetShowing(object show)
       {
           this.Showing = (bool) show;
       }
       /// <summary>
       /// 控制精灵不显示，鼠标在精灵显示区域内则不关闭
       /// </summary>
       public virtual void UnShowByAre(object args)
       {
            //判断鼠标位置已经位于图片按钮矩形区域内，如果在则不关闭显示
           if (MouseState.X > DrawPosition.X && MouseState.X < DrawPosition.X + DrawPosition.Width
               && MouseState.Y > DrawPosition.Y && MouseState.Y < DrawPosition.Y + DrawPosition.Height)
           {
               return;
           }
           this.Showing = false;
       }
#region 精灵事件监听
       /// <summary>
       /// 鼠标左键单击事件监听器
       /// </summary>
       /// <param name="args">传递的任意参数</param>
       public void ClickLeftEventListener(object args)
       {
           //判断鼠标位置已经位于图片按钮矩形区域内
           if (this.Showing&&MouseState.X > DrawPosition.X && MouseState.X < DrawPosition.X + DrawPosition.Width
               && MouseState.Y > DrawPosition.Y && MouseState.Y < DrawPosition.Y + DrawPosition.Height)
           {
               //鼠标左键按下记录上一个状态，防止短时间内多次触发事件
               if (MouseState.LeftButton == ButtonState.Pressed)
               {
                   this.IsLeftButtonPressed = true;
               }
               if (MouseState.LeftButton == ButtonState.Released && this.IsLeftButtonPressed)
               {
                   this.IsLeftButtonPressed = false;
                   if (ClickLeftEvent != null) ClickLeftEvent(args);
                  
               }
           }
       }
       /// <summary>
       /// 鼠标右键单击事件监听器
       /// </summary>
       /// <param name="args">传递的任意参数</param>
       public void ClickRightEventListener(object args)
       {
           //判断鼠标位置已经位于图片按钮矩形区域内
           if (this.Showing && MouseState.X > DrawPosition.X && MouseState.X < DrawPosition.X + DrawPosition.Width
               && MouseState.Y > DrawPosition.Y && MouseState.Y < DrawPosition.Y + DrawPosition.Height)
           {
               //鼠标右键按下记录上一个状态，防止短时间内多次触发事件
               if (MouseState.RightButton == ButtonState.Pressed)
               {
                   this.IsRightButtonPressed = true;
               }
               if (MouseState.RightButton == ButtonState.Released && this.IsRightButtonPressed)
               {
                   if (ClickRightEvent != null) ClickRightEvent(args);
                   this.IsRightButtonPressed = false;
               }
           }
       }
       /// <summary>
       /// 鼠标移入事件监听器
       /// </summary>
       /// <param name="args">传递的任意参数</param>
       public void MoveOnEventListener(object args)
       {
           if (this.IsMoveOn)
               return;
           //判断鼠标位置已经位于图片按钮矩形区域内
           if (MouseState.X > DrawPosition.X && MouseState.X < DrawPosition.X + DrawPosition.Width
               && MouseState.Y > DrawPosition.Y && MouseState.Y < DrawPosition.Y + DrawPosition.Height)
           {
               if (MoveOnEvent != null) MoveOnEvent(args);
               this.IsMoveOn = true;
           }
       }
       /// <summary>
       /// 鼠标移出事件监听器
       /// </summary>
       /// <param name="args">传递的任意参数</param>
       public void MoveOutEventListener(object args)
       {
           if (!this.IsMoveOn)
               return;
           //判断鼠标位置已经位于图片按钮矩形区域内
           if (MouseState.X < DrawPosition.X || MouseState.X > DrawPosition.X + DrawPosition.Width
               || MouseState.Y < DrawPosition.Y|| MouseState.Y > DrawPosition.Y + DrawPosition.Height)
           {
               if (MoveOutEvent != null) MoveOutEvent(args);
               this.IsMoveOn = false;
           }
       }
#endregion
       /// <summary>
       /// 鼠标左键单击事件处理器
       /// </summary>
       /// <param name="args">事件参数，多项使用逗号隔开</param>
       public virtual void ClickLeftEventHandler(object args){}
   }
}
